package cn.edu.jxau.test;

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.concurrent.ExecutionException;

public class Main {

	public static void main(String[] args) throws InterruptedException, ExecutionException {

//		Integer[] arr = new Integer[] { 1, 2, 3, 4 };
		Integer[] arr = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		Node<Integer> head = toLinkedList(arr);
		print(head);
		print(foo(head));
	}

	private static <T> Node<T> foo(Node<T> head) {

		Objects.requireNonNull(head, "参数head不能为null");
		Node<T> pre = null;
		Node<T> cur = head;
		while (cur != null) {
			Node<T> next = cur.next;
			cur.next = pre;
			pre = cur;
			cur = next;
		}
		return pre;
	}

	private static <T> void print(Node<T> head) {

		Node<T> cur = head;
		while (cur != null) {
			System.out.print(cur.data + "\t");
			cur = cur.next;
		}
	}

	private static <T> Node<T> toLinkedList(T[] arr) {

		Objects.requireNonNull(arr, "参数arr不能为null");
		if (arr.length == 0) {
			return null;
		}

		Node<T> head = new Node<>(arr[0]);
		Node<T> cur = head;
		for (int i = 1; i < arr.length; i++) {
			Node<T> node = new Node<>(arr[i]);
			cur.next = node;
			cur = node;
		}
		return head;
	}
}

class Node<T> {

	public T data;
	public Node<T> next;

	public Node() {

	}

	public Node(T data) {
		this.data = data;
	}
}